package com.example.java_service.impl;

import com.example.java_dao.UserDao;
import com.example.java_pojo.User;
import com.example.java_service.UserService;
import com.example.java_util.exception.BusinessException;
import com.example.java_util.utils.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@ComponentScan({
        "com.example.java_dao",
        "com.example.java_util"})
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    public boolean save(User user) {
        userDao.save(user);
        return true;
    }

    public boolean update(User user) {
        userDao.update(user);
        return true;
    }

    public boolean delete(Integer id) {
        userDao.delete(id);
        return true;
    }

    public User findById(Integer id) {
        //转化为自定义异常
        if (id < 0) {
            throw new BusinessException(Code.BUSINESS_ERR, "非法操作");
        }
        return userDao.findById(id);
    }

    public List<User> findAll() {
        return userDao.findAll();
    }

    public User findByName(String name) {
        return userDao.findByName(name);
    }

    public User login(String username, String password) {
        return userDao.select(username, password);
    }
}
